/*
 *  Copyright 2010 sonrisa.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.zh.jbet.core.entity;

import com.zh.jbet.core.util.JAXBUtil;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 *
 * @author sonrisa
 */
@Entity
@Table(name = "PLAYERGAME")
public class PlayerGame implements BaseEntity, Serializable {
    private Long id;
    private GameContext gameContext;
    private Player owner;
    private String scorerXml;
    private BetScorers betScorers;

    @Id
    @SequenceGenerator(name = "S_PLAYERGAME", sequenceName = "S_PLAYERGAME", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "S_PLAYERGAME")
    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    @ManyToOne()
    @JoinColumn(name="OWNERID")
    public Player getOwner() {
        return owner;
    }

    public void setOwner(Player owner) {
        this.owner = owner;
    }

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "SCORERXML", columnDefinition = "CLOB NOT NULL")
    protected String getScorerXml() {
        return scorerXml;
    }

    protected void setScorerXml(String scorerXml) {
        this.scorerXml = scorerXml;
    }

    public <T extends Game> BetScorers<T> getGameScorer(Class<T> gameClass) {
        if (betScorers == null) {
            betScorers = JAXBUtil.unMarshalScorer(new StringReader(getScorerXml()), gameClass);
        }
        return betScorers;
    }

    public <T extends Game> void setBetScorers(BetScorers<T> betScorers, Class<T> gameClass) {
        this.betScorers = betScorers;
        StringWriter writer = new StringWriter();
        JAXBUtil.marshalScorer(betScorers, gameClass, writer, true);
        setScorerXml(writer.toString());
    }
    
    @ManyToOne()
    @JoinColumn(name="CONTEXTID")
    public GameContext getGameContext() {
        return gameContext;
    }

    public void setGameContext(GameContext gameContext) {
        this.gameContext = gameContext;
    }
}
